package com.yehuo.app.schedule;


import com.alibaba.fastjson.JSON;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayTask implements Delayed {
    private final TaskBase data;
    private final long expire;

    public DelayTask(TaskBase data, long expire, TimeUnit unit) {
        this.data = data;
        this.expire = System.currentTimeMillis() + (expire > 0L ? unit.toMillis(expire) : 0L);
    }

    public DelayTask(TaskBase data, long expire) {
        this.data = data;
        this.expire = expire;
    }

    public TaskBase getData() {
        return this.data;
    }

    public long getExpire() {
        return this.expire;
    }

    public boolean equals(Object obj) {
        return obj instanceof DelayTask ? this.data.getIdentifier().equals(((DelayTask)obj).getData().getIdentifier()) : false;
    }

    public long getDelay(TimeUnit unit) {
        return this.expire - System.currentTimeMillis();
    }

    public int compareTo(Delayed o) {
        DelayTask delayTask = (DelayTask)o;
        long diff = this.expire - delayTask.expire;
        return diff <= 0L ? -1 : 1;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }
}

